package com.tony.glyphhackhelper;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;

import wei.mark.standout.StandOutWindow;

public class ScreenCaptureActivity extends Activity {

    private MediaProjectionManager mMediaProjectionManager;
    private static final int REQUEST_MEDIA_PROJECTION = 1;


    /** Called when the activity is first created. */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StandOutWindow.closeAll(this, ScreenCaptureService.class);

        // show a MultiWindow, ScreenCaptureService
        mMediaProjectionManager = (MediaProjectionManager)getApplication().getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        ((ScreenCaptureApplication)getApplication()).setMediaProjectionManager(mMediaProjectionManager);
        //Start screen capture service
        StandOutWindow
                .show(this, ScreenCaptureService.class, StandOutWindow.DEFAULT_ID);
        startActivityForResult(
                mMediaProjectionManager.createScreenCaptureIntent(),
                REQUEST_MEDIA_PROJECTION);

    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (resultCode != Activity.RESULT_OK) {
                return;
            }else if(data != null && resultCode != 0){
                ((ScreenCaptureApplication)getApplication()).setResult(resultCode);
                ((ScreenCaptureApplication)getApplication()).setIntent(data);

            }
        }
        finish();
    }
}
